/*
 *
 *
Copyright (c) 1998-2001 Aristar, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:

"This product includes software developed by Aristar, Inc. (http://www.aristar.com/)."

Alternately, this acknowledgment may appear in the software itself, if and
wherever such third-party acknowledgments normally appear.

4. The names "Dialect" and "Aristar, Inc." must not be used to endorse or
promote products derived from this software without prior written permission.
For written permission, please contact info@aristar.com.

5. Products derived from this software may not be called "Dialect", nor may
"Dialect" appear in their name, without prior written permission of Aristar, Inc.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
ARISTAR, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software consists of voluntary contributions made by many individuals
on behalf of Aristar, Inc.  For more information on Aristar, Inc.,
please see <http://www.aristar.com/>.

Portions of this software are based upon software freely available and
written by individuals not associated with Aristar, Inc.  Copyrights are
maintained in the relevant source code files.
 *
 *
 */

#include "DConfigGlobals.h"

#if defined(ADO_SUPPORT) && !defined(__ADOCLASSES__)
#define __ADOCLASSES__

#ifdef UNDER_CE
//#	include <comutil.h> for _bstr_t and _variant_t
#	include "adoce.h"
#	define FIELDPTR			IADOCEField*
#	define FIELDSPTR		IADOCEFields*
#	define RECORDSETPTR		IADOCERecordset*
//#	define PROPERTYPTR		IADOCEProperty*
//#	define PROPERTIESPTR	IADOCEProperties*
#else
#	import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#	define FIELDPTR			FieldPtr
#	define FIELDSPTR		FieldsPtr
#	define RECORDSETPTR		_RecordsetPtr
#	define PROPERTYPTR		PropertyPtr
#	define PROPERTIESPTR	PropertiesPtr
#endif

class TComObject {
public:
	TComObject();
	virtual ~TComObject();
	TComObject(const TComObject& copy);
	TComObject& operator = (const TComObject& copy);
	CString GetLastError();
private:
	static long m_RefCount;
protected:
	CString m_LastError;
};

/////////////////////////////////////////////////////
#ifndef UNDER_CE

class TADOProperty : public TComObject {
public:
	PROPERTYPTR m_pProperty;

#ifdef UNDER_CE
	TADOProperty();
	virtual ~TADOProperty();
#endif

	bool GetName(CString* name);
	bool GetType(DataTypeEnum* type);
//#ifdef UNDER_CE
	bool GetValue(VARIANT* value);
	bool SetValue(VARIANT value);
//#else
//	bool GetValue(VARIANT* value);
//	bool SetValue(_variant_t value);
//#endif
};


class TADOProperties : public TComObject {
public:
	PROPERTIESPTR m_pProperties;

#ifdef UNDER_CE
	TADOProperties();
	virtual ~TADOProperties();
#endif

	bool GetCount(long* count);
	bool GetItem(long index, TADOProperty* p);
	bool GetItem(LPCTSTR name, TADOProperty* p);
};
#endif // !UNDER_CE

/////////////////////////////////////////////////////

class TADOField : public TComObject {
public:
	FIELDPTR m_pField;

#ifdef UNDER_CE
	TADOField();
	virtual ~TADOField();
#endif

	bool GetActualSize(long* size);
	bool GetDefinedSize(long* size);
	bool GetName(CString* name);
	bool GetType(DataTypeEnum* type);
	bool GetAttributes(long* attrib);
#ifndef UNDER_CE
	bool GetProperties(TADOProperties* props);
#endif
//#ifdef UNDER_CE
	bool GetValue(VARIANT* value);
	bool SetValue(VARIANT value);
//#else
//	bool GetValue(VARIANT* value);
//	bool SetValue(_variant_t value);
//#endif
};

/////////////////////////////////////////////////////

class TADOFields : public TComObject {
public:
	FIELDSPTR m_pFields;

#ifdef UNDER_CE
	TADOFields();
	virtual ~TADOFields();
#endif

	bool GetCount(long* count);
	bool GetItem(long index, TADOField* p);
	bool GetItem(LPCTSTR name, TADOField* p);
};

/////////////////////////////////////////////////////

/////////////////////////////////////////////////////

#ifndef UNDER_CE
class TADOConnection : public TComObject {
public:
	TADOConnection();
	virtual ~TADOConnection();
	_ConnectionPtr m_pCon;

	bool Open(LPCTSTR connectString, LPCTSTR userID = _T(""),
		LPCTSTR password = _T(""), long options = -1);
	bool Execute(LPCTSTR command, long* numRecsAffected, long options);
	bool Close();

	bool GetConnectionString();
	bool SetConnectionString();
	bool GetCommandTimeout();
	bool SetCommandTimeout();
	bool GetConnectTimeout();
	bool SetConnectTimeout();
	bool GetCursorLocation();
	bool SetCursorLocation(CursorLocationEnum loc);
	bool GetDefaultDatabase();
	bool SetDefaultDatabase();
	bool GetProvider(CString* provider);
	bool SetProvider(CString provider);
	bool GetState(long* state);
    bool BeginTrans(long* result);
    bool CommitTrans();
    bool RollbackTrans();

};
#endif // UNDER_CE

/////////////////////////////////////////////////////

class TADORecordset : public TComObject {
public:
	TADORecordset();
	virtual ~TADORecordset();
	RECORDSETPTR m_pRs;

	// Functions
	bool AddNew();
	bool Open(LPCTSTR command, LPCTSTR conString, long cursorType, long lockType, long options);
#ifdef UNDER_CE
	bool Delete(long affect = 1);
#else
	bool Open(LPCTSTR command, TADOConnection& con, long cursorType, long lockType, long options);
	bool Delete(AffectEnum affect = adAffectCurrent);
	bool GetProperties(TADOProperties* props);
	bool Find(LPCTSTR criteria, long skipRecs = 0,
		SearchDirectionEnum srchDir = adSearchForward,
		_variant_t start = (long)adBookmarkCurrent);
#endif
	bool Close();
	bool GetFields(TADOFields* fields);
	bool GetRows();
	bool GetString(long stringFormat, long numRows, LPCTSTR colDelimeter,
		LPCTSTR rowDelimeter, LPCTSTR nullExp, CString* result);
	bool Move();
	bool MoveFirst();
	bool MoveLast();
	bool MoveNext();
	bool MovePrevious();
	bool Requery();
	bool Resync();
	bool Save();
	bool Supports();
	bool Update();
	bool CancelUpdate();

	// Properties
	bool GetAbsolutePage();
	bool SetAbsolutePage();
	bool GetAbsolutePosition(long* pos);
	bool SetAbsolutePosition(long pos);
	bool GetBOF(bool* bBOF);
	bool GetEOF(bool* bEOF);
#ifndef UNDER_CE
	bool GetCursorLocation();
	bool SetCursorLocation(CursorLocationEnum loc);
#endif
	bool GetCursorType();
	bool SetCursorType();
	bool GetEditMode();
	bool SetEditMode();
	bool GetLockType();
	bool SetLockType();
	bool GetRecordCount(long* size);
	bool GetSort();
	bool SetSort();
	bool GetSource();
	bool SetSource();
	bool GetState();
	bool GetStatus();
};

#endif // __ADOCLASSES__
